package swd.maintain.system.sync_db_table;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;

public class Action {
    QueryBean gqbnSrcDAO = null;
    QueryBean gqbnTrgDAO = null;
    public String action(javax.servlet.jsp.PageContext pageContext) {
        HttpServletRequest request = (HttpServletRequest) pageContext.
                                     getRequest();
        HttpServletResponse response = (HttpServletResponse) pageContext.
                                       getResponse();

        View view = (View) request.getAttribute("view");
        (new WebFunction()).setPortal_to_session(request); //set session from portal
        if (request.getSession().getAttribute("u_id") == null) {
            try {
                request.getRequestDispatcher((new StringBuffer("login.jsp")).
                                             insert(0, "/").toString()).forward(
                        request, response);
                return "login";
            } catch (Exception le) {
                view.setOnloadScript(
                        "can not redirect to login page...please contact IT");
                return "";
            }
        }
        String lstrUserNO = request.getSession().getAttribute("u_id").toString();

        String lstrResult = null;

        try {
            if(view.getSrc_db_alias()!=null && !view.getSrc_db_alias().trim().equals("")){
                gqbnSrcDAO = new QueryBean(view.getSrc_db_alias(), view.getDBDebug(),
                                view.getSourceCharset(), view.getTargetCharset());
            }

            if(view.getTrg_db_alias()!=null && !view.getTrg_db_alias().trim().equals("")){
                gqbnTrgDAO = new QueryBean(view.getTrg_db_alias(), view.getDBDebug(),
                                view.getSourceCharset(), view.getTargetCharset());
            }

            Business business = new Business(gqbnSrcDAO,gqbnTrgDAO);

            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("getData")) {//透過ajax get data
                lstrResult = business.getData(view, lstrUserNO);

                pageContext.getOut().clear();
                pageContext.getOut().write(lstrResult);
                pageContext.getOut().flush();
                return "login";
            }

            business.setDB_alias_list(view);

            if(view.getPrgAction().equals("transfer")){
                business.transferDB(view);
            }

            return "";
        } catch (Exception e) {
            e.printStackTrace();
            view.setOnloadScript("alert('Program error please contact IT.');");
            return "";
        } finally {
            try {
                if (gqbnSrcDAO != null) {
                    gqbnSrcDAO.close();
                    gqbnSrcDAO=null;
                }
            } catch (Exception e) {
                System.out.println("can not close querybean in Action.java");
            }

            try {
                if (gqbnTrgDAO != null) {
                    gqbnTrgDAO.close();
                    gqbnTrgDAO=null;
                }
            } catch (Exception e) {
                System.out.println("can not close querybean in Action.java");
            }
        }
    }
}
